package com.hongli.blog.controller;

import com.hongli.blog.config.CookieUtils;
import com.hongli.blog.redisconfig.RedisUtil;
import com.hongli.blog.config.Result;
import com.hongli.blog.entity.BasicsDao;
import com.hongli.blog.entity.UserDao;
import com.hongli.blog.mapper.Basicsmapper;
import com.hongli.blog.mapper.Rolemapper;
import com.hongli.blog.mapper.Usermapper;
import com.hongli.blog.scheduledtask.InitializationController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 用户控制器
 * @Author ZERO
 * @Date 2021/11/8 12:01
 **/
@RestController
@RequestMapping(value = "/login")
@CrossOrigin
public class LoginController {

    @Autowired
    private Usermapper usermapper;

    @Autowired
    private Rolemapper rolemapper;

    @Autowired
    private Basicsmapper basicsmapper;

    @Autowired
    private RedisUtil redisUtil;

    public String COOKIE_BLOG = "blog";

    /** 返回基础数据
      * @author ZERO
      * @date 2021/11/10 16:07
    **/
    @PostMapping(value = "/returnBasics")
    public Object ReturnBasics(){
        HashMap<String,Object> map = new HashMap<>();
        List<BasicsDao> listbasicsall = basicsmapper.SelectAllBasics();
        for(BasicsDao b : listbasicsall){
            if("1".equals(b.getChoice())){
                map.put("mainTitle",b.getMainTitle());
                map.put("author",b.getAuthor());
                map.put("meaningtime",new InitializationController().Times(b.getMeaningtime()));
                map.put("record",b.getRecord());
                break;
            }
        }
        return Result.succ(map);
    }

    /** 登出
      * @author ZERO
      * @date 2021/11/11 11:49
    **/
    @PostMapping(value = "/LogOut")
    public Result LogOut(HttpServletRequest request,HttpServletResponse response){
        try{
            if("null".equals(CookieUtils.getCookie(request,COOKIE_BLOG).trim())
                    || "".equals(CookieUtils.getCookie(request,COOKIE_BLOG).trim())){
                return Result.fail("1000");
            }
            String S = CookieUtils.getCookie(request,COOKIE_BLOG).trim();
            if(redisUtil.hasKey(S)){
                CookieUtils.destroyCookies(response,COOKIE_BLOG);
                redisUtil.del(S);
                return Result.fail("2000");
            }else return Result.fail("3000");
        }catch (Exception e){
            return Result.fail("4000");
        }
    }

    /** 查询用户，登录
      * @author ZERO
      * @date 2021/11/11 14:28
    **/
    @PostMapping(value = "/ReturnUserName")
    public Result loginUser(@RequestBody Map<String, Object> map,HttpServletResponse response){
        String N = map.get("username").toString().trim(),
                P = map.get("password").toString().trim(),
                R = map.get("radio3").toString().trim();
        if(N.trim().length() <= 0 && P.trim().length() <= 0){
            return Result.fail("1000");
        }
        String pass = DigestUtils.md5DigestAsHex(P.getBytes());
        UserDao userDao = usermapper.SelectUser(N,pass);
        if(userDao == null){
            return Result.fail("3000");
        }
        //是否记住？
        if("1".equals(R)){
            CookieUtils.writeCookie(response,COOKIE_BLOG,COOKIE_BLOG+userDao.getUserid());
            redisUtil.set(COOKIE_BLOG+userDao.getUserid(),String.valueOf(userDao.getUserid()).trim());
        }
        return Result.fail("2000",userDao.getUsername());
    }

    /** 登录验证
      * @author ZERO
      * @date 2021/11/11 17:00
    **/
    @GetMapping(value = "/SelectRedisUser")
    public Result SelectRedisUser(HttpServletRequest request){
        //cookie(a1,a2) ==> redis(a2,a3)
        try{
            if(CookieUtils.getCookie(request,COOKIE_BLOG).trim() == null
                    || "".equals(CookieUtils.getCookie(request,COOKIE_BLOG).trim())){
                return Result.fail("1000");
            }
            String S = CookieUtils.getCookie(request,COOKIE_BLOG).trim();
            if(redisUtil.hasKey(S)){
                UserDao userDao = usermapper.SelectUserName(Integer.parseInt(redisUtil.get(S).trim()));
                if(userDao == null) return Result.fail("3000");
                return Result.fail("2000",userDao.getUsername());
            }else return Result.fail("3000");
        }catch (Exception e){
            return Result.fail("1000");
        }
    }
}
